<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref} from 'vue'
import { TableData } from '@/api/table/types'
import { useValidator } from '@/hooks/web/useValidator'
import { FormSchema } from '@/types/form'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<TableData>>,
    default: () => null
  },
  formSchema: {
    type: Array as PropType<FormSchema[]>,
    default: () => []
  }
})
//时间
let format = 'YYYY-mm-dd HH:MM:SS'
let date = new Date();
const dataItem = {
  'Y+': date.getFullYear().toString(),
  'm+': (date.getMonth() + 1).toString(),
  'd+': date.getDate().toString(),
  'H+': date.getHours().toString(),
  'M+': date.getMinutes().toString(),
  'S+': date.getSeconds().toString(),
};
Object.keys(dataItem).forEach((item) => {
  const ret = new RegExp(`(${item})`).exec(format);
  if (ret) {
    format = format.replace(ret[1], ret[1].length === 1 ? dataItem[item] : dataItem[item].padStart(ret[1].length, '0'));
  }
});

const rules = reactive({
  title: [required()],
  author: [required()],
  importance: [required()],
  pageviews: [required()],
  display_time: [required()],
  content: [required()]
})
let searchSchema = reactive<FormSchema[]>([
  {
    field: 'deptName',
    label: '部门名称',
    component: 'Input',
    formItemProps: {
      rules: [required()]
    }
  },
  {
    field: 'deptStatus',
    label: '是否启用',
    component: 'Select',
    componentProps: {
      options: [
        {
          label:'请选择状态',
          value:''
        },
        {
          label:'启用',
          value:1
        },
        {
          label:'停用',
          value:2
        }
      ]
    },
    formItemProps: {
      rules: [required()]
    }
  },
  {
    field: 'createDate',
    label: '创建时间',
    component:'Input',
    componentProps:({
      disabled: true,
      value:format
    })
  },
  {
    field: 'deptDescirbe',
    label: '部门描述',
    component: 'Input',
  }
])
const { register, methods, elFormRef } = useForm({
  schema: searchSchema
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues } = methods
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules" @register="register" :isCol="false"/>
</template>
